<?php

namespace App\Http\Middleware;

use Closure;
use Route;
use Auth;
class CheckRbac
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //可以在这里写需要代码
        $actions = strtolower(Route::currentRouteAction());
        //echo $actions;die;        //app\http\controllers\admin\indexcontroller@index
        $ac = strtolower(Auth::guard('admin') -> user() -> rel_role() -> first() -> auth_ac);
        //添加例外
        $ac = $ac . ',indexcontroller@index,indexcontroller@welcome,publiccontroller@logout';
        //判断权限是否存在
        $action = explode('\\',$actions);
        //end方法的参数必须是变量，empty  isset
        if(strrpos($ac,end($action)) ===  false){
            //为了后面方便开发，可以临时将权限判断注掉
            // return redirect('/admin/public/login') -> withErrors([
            //         'error' =>  '您没有访问权限',
            //     ]);exit;
        }
        //到这里结束
        return $next($request);
    }
}
